package com.woniu.ly.ticketassembly.controller;

import com.woniu.ly.model.common.Result;
import com.woniu.ly.model.model.Product;
import com.woniu.ly.model.model.ProductImg;
import com.woniu.ly.model.model.ProductScheduing;
import com.woniu.ly.ticketassembly.service.ProductService;
import com.woniu.ly.ticketfacade.ProductManageAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 录入产品方式：
 * 1.先录入产品基本信息，保存并提交
 * 2.提交成功后提示录入产品图片和日程安排
 * 3.日程以一个节点一个节点的方式提交
 * 4.图片是多张图片同时上传
 */

@Slf4j
@RestController
public class ProduceController implements ProductManageAPI {

    @Autowired
    private ProductService productService;

    private int productId = -1;

    //添加产品基本信息
    public Result makeProduct(String productName, String productIntro, String productCostIncludes,
                              double productPrice, int productMinPeople, String productType){
        Product product = new Product();
        product.setProductName(productName);
        product.setProductIntro(productIntro);
        product.setProductCostIncludes(productCostIncludes);
        product.setProductPrice(productPrice);
        product.setProductMinPeople(productMinPeople);
        product.setProductType(productType);
        // 新增产品数据库持久化返回自增id
        productId = productService.makeProduct(product);
        if(productId>0){
            return Result.success("制定产品成功");
        }
        return Result.fail("制定产品失败");
    }

    //添加产品日程（单个节点添加）
    public Result addProductScheduing(int hotelId,int spotId,String timeNode,int scheduingSerialNum) {
        if (productId > 0) {
            ProductScheduing productScheduing = new ProductScheduing();
            productScheduing.setProductId(productId);
            productScheduing.setHotelId(hotelId);
            productScheduing.setSpotId(spotId);
            productScheduing.setTimeNode(timeNode);
            productScheduing.setScheduingSerialNum(scheduingSerialNum);
            productService.addProductScheduing(productScheduing);
            return Result.success("添加行程成功");
        }
        return Result.fail("添加行程失败");
    }

    //添加产品图片（多张图片上传）
    public Result addProductImg(int productId,List<MultipartFile> productImgFiles){
        for (MultipartFile productImgFile : productImgFiles) {
            String originalFilename = productImgFile.getOriginalFilename();
            String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
            String filename = UUID.randomUUID() + fileType;
            try {
                productImgFile.transferTo(new File("本地路径地址" + filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
            ProductImg productImg = new ProductImg();
            productImg.setProductId(productId);
            productImg.setProductImgUrl("Nginx上的路径地址" + filename);
            productService.addProductImg(productImg);
            return Result.success("图片上传成功");
        }
        return Result.fail("图片上传失败");
    }

    //根据产品id查询产品
    public Result getProductById(int productId){
        Product product = productService.getProductById(productId);
        return Result.success(product);
    }

    //查询所有产品
    public Result getAllProducts(){
        List<Product> products = productService.getAllProducts();
        return Result.success(products);
    }

    //根据产品id查询产品日程安排
    public Result getProductScheduingById(int productId){
        List<ProductScheduing> productScheduings = productService.getProductScheduingById(productId);
        return Result.success(productScheduings);
    }

    //根据产品id查询产品图片
    public Result getProductImgById(int productId){
        List<ProductImg> productImgs = productService.getProductImgById(productId);
        return Result.success(productImgs);
    }

    //根据产品id修改产品
    public Result modifyProductById(int productId,String productName, String productIntro, String productCostIncludes,
                                    double productPrice, int productMinPeople, String productType){
        Product product = new Product();
        product.setProductId(productId);
        product.setProductName(productName);
        product.setProductIntro(productIntro);
        product.setProductCostIncludes(productCostIncludes);
        product.setProductPrice(productPrice);
        product.setProductMinPeople(productMinPeople);
        product.setProductType(productType);
        productService.modifyProductById(product);
        return Result.success("修改成功");
    }

    //根据产品id修改产品行程安排
    public Result modifyProductScheduingById(int productScheduingId,int hotelId,int spotId,String timeNode,int scheduingSerialNum){
        ProductScheduing productScheduing = new ProductScheduing();
        productScheduing.setHotelId(hotelId);
        productScheduing.setSpotId(spotId);
        productScheduing.setTimeNode(timeNode);
        productService.modifyProductScheduingById(productScheduing);
        return Result.success("修改成功");
    }





}
